package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 董万均
 * @since 2020-11-03
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String loginConfirmation(UcenterMember member) {
        //获取用户输入的手机号和密码，也就是请求发送过来的
        String mobile = member.getMobile();
        String password = member.getPassword();
        //判断手机号和密码输入的是否为null
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"用户名或者密码不为空");
        }
        //当手机号和密码都不为空，开始验证该用户是否注册，也就是数据库中有没有该用户的信息
        //根据手机号查询到该用户
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember==null){
            throw new GuliException(20001,"该用户没有注册");
        }
        //判断密码，数据库中的密码都是进MD5进行加密后的字符串，直接进行判断是无法通过，需要将输入的密码进行加密
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new GuliException(20001,"密码输入错误");
        }

        //判断该用户是否被禁用
        if (mobileMember.getIsDisabled()){
            throw new GuliException(20001,"该用户已经被禁止登陆");
        }

        //登陆成功之后，将用户信息进行JWT生成一定的字符串
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    //用户注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取用户的注册信息
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码

        //判断输入的用户信息是否为空
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) ||
               StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册信息不能为空");
        }

        //判断该验证码是否输入正确，发送的验证是存储在redis中，从redis中获取验证码
        String redisCode=redisTemplate.opsForValue().get(mobile);//在RedisTemplate<String,String>中设置key和value值的类型
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"验证码输入错误");
        }

        //判断该用户是否注册
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new GuliException(20001,"该用户已经被注册");
        }

        //将注册的信息放入到数据库中
        UcenterMember member=new UcenterMember();
        member.setNickname(nickname);
        member.setIsDisabled(false);//表示未被禁用
        member.setPassword(MD5.encrypt(password));//将密码进行加密
        member.setAvatar("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2731749687,1375823788&fm=11&gp=0.jpg");
        member.setMobile(mobile);
        baseMapper.insert(member);
    }

    //使用openid判断微信用户是否登录
    @Override
    public UcenterMember selectByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer registerCount(String day) {
        return baseMapper.registerCount(day);
    }
}
